package com.study.base;

/**
 * @description: ThreadTest  多次线程
 * @date: 2020/11/12 10:42
 * @author: hcm
 * @version: 1.0
 */
public class ThreadTest  {

    public static void main(String[] args) {
        //子线程运行执行是次后，主线程再运行五次
        final  Bussiness bussiness = new Bussiness();
        //子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    bussiness.subMethod();
                }
            }
        }).start();
        //主线程
        for (int i = 0; i < 3; i++) {
            bussiness.mainMethod();
        }
    }
static class Bussiness{
        public boolean subFlag = true;
        public synchronized void mainMethod(){
            while (subFlag){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName()+": main thread running loop count --"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            subFlag = true;
            notify();
        }

    public synchronized void subMethod() {
        while (!subFlag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+": sub thread running loop count -- " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        subFlag = false;
        notify();
    }
}
}
